/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：OrderService.java
 * History:
 *       Apr 16, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.service.admin;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.dao.IOrderDao;
import com.xteam.ecsystem.dao.IOrderLogDao;
import com.xteam.ecsystem.dao.ISystemLogDao;
import com.xteam.ecsystem.entities.BaseOrderInfo;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.OrdersQueryCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 订单信息业务管理层
 */
@Service
@SuppressWarnings("unchecked")
public class OrderService {
	
	@Autowired
	public IOrderDao orderDao;
	
	@Autowired
	public IOrderLogDao orderLogDao;
	
	@Autowired
	public ISystemLogDao sysLogDao;
	
	/**
	 * 查询并分页显示订单信息【查询所有用户的订单信息】
	 * @author liuzhi Nov 6, 2013
	 * @param direction
	 * @param orderStation
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public String getAllOrderList(String direction, OrdersQueryCenterStation orderStation,
			int pageIndex, int pageSize) {
		return this.orderDao.getAllOrderList(direction, orderStation, pageIndex, pageSize);
	}
	
	/**
	 * 
	 * 查询并分页显示订单信息【查询注册用户的订单信息】
	 * @author liuzhi
	 * @param direction
	 * @param orderStation
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public String getOrderList(String direction, OrdersQueryCenterStation orderStation, 
			int pageIndex, int pageSize){
		return this.orderDao.getOrderList(direction, orderStation, pageIndex, pageSize);
	}
	
	/**
	 * 根据区域编号获得区域信息
	 * @author liuzhi
	 * @param regionId
	 * @return
	 */
	public String getRegionInfo(String regionId){
		return this.orderDao.getRegionInfo(regionId);
	}
	
	/**
	 * 
	 * 根据订单id获得订单信息
	 * @author liuzhi
	 * @param id
	 * @return
	 */
	public String getOrderInfoById(String id){
		return this.orderDao.getOrderInfoById(id);
	}
	
	/**
	 * 
	 * 根据会员id获得会员信息
	 * @author liuzhi
	 * @param id
	 * @return
	 */
	public String getMemberInfoById(String id){
		return this.orderDao.getMemberInfoById(id);
	}
	
	/**
	 * 根据订单编号加载商品订购列表
	 * @author liuzhi
	 * @param id
	 * @return
	 */
	public String getOrderGoodsById(String id){
		return this.orderDao.getOrderGoodsById(id);
	}
	
	/**
	 * 根据订单编号和操作类型处理订单状态
	 * @author liuzhi
	 * @param id
	 * @param disposeType
	 * @param lolUtype
	 * @param lolUser
	 * @param lolAddress
	 * @param lolOremark
	 * @param boiCreason
	 * @param boiIsrefund
	 * @param boiRefundremark
	 * @param boiInvnum
	 * @return str
	 */
	public String saveDisposedOrder(String id, String disposeType, String lolUtype,
			String lolUser, String lolAddress, String lolOremark, String boiCreason,
			String boiIsrefund, String boiRefundremark, String boiInvnum){
		BaseOrderInfo order = this.orderDao.saveDisposedOrder(id, disposeType, boiCreason, 
				boiIsrefund, boiRefundremark, boiInvnum);
		
		if (order != null) {
			String lolOtype = "";
			if (ConstantLocation.BTN_QUE_R[0].equals(disposeType)) {
				lolOtype = ConstantLocation.BTN_QUE_R[1];
				
			}else if(ConstantLocation.BTN_FU_K[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_FU_K[1];
				
			}else if(ConstantLocation.BTN_PEI_H[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_PEI_H[1];
				
			}else if(ConstantLocation.BTN_FA_H_D[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_FA_H_D[1];
				
			}else if(ConstantLocation.BTN_QU_F_H[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_QU_F_H[1];
				
			}else if(ConstantLocation.BTN_SHOU_H[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_SHOU_H[1];
				
			}else if(ConstantLocation.BTN_TUI_H[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_TUI_H[1];
				
			}else if(ConstantLocation.BTN_QU_X[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_QU_X[1];
				
			}else if(ConstantLocation.BTN_WU_F_L_X[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_WU_F_L_X[1];
				
			}else if(ConstantLocation.BTN_SHI_X[0].equals(disposeType)){
				lolOtype = ConstantLocation.BTN_SHI_X[1];
				
			}
			
			this.orderLogDao.saveOrderLog(lolOtype, id, lolUtype, lolUser, lolAddress,
					new Date(), lolOremark);
			
			return JSONObject.fromObject(order).toString();
		}
		
		return "";
		
	}
	
	/**
	 * 
	 * 批量确认订单信息
	 * @author liuzhi
	 * @param ids
	 * @param lolUtype
	 * @param lolUser
	 * @param lolAddress
	 */
	public void saveQueRenOrder(String ids, String lolUtype, String lolUser, String lolAddress){
		this.orderDao.saveQueRenOrder(ids);
		
		String oId[] = ids.split(",");
		for (String id : oId) {
			this.orderLogDao.saveOrderLog(ConstantLocation.LOG_BATCH_QUE_R, id, lolUtype, lolUser, 
					lolAddress, new Date(), ConstantLocation.LOG_BATCH_QUE_R);
		}
		
	}
	
	/**
	 * 
	 * 批量取消订单信息
	 * @author liuzhi
	 * @param ids
	 * @param lolUtype
	 * @param lolUser
	 * @param lolAddress
	 * @param lolOremark
	 * @param boiCreason
	 * @param boiIsrefund
	 * @param boiRefundremark
	 */
	public void saveQuXiaoOrder(String ids, String lolUtype, String lolUser, String lolAddress,
			String lolOremark, String boiCreason, String boiIsrefund, String boiRefundremark){
		if(StringUtil.isNotNullOrEmpty(ids)){
			this.orderDao.saveQuXiaoOrder(ids, boiCreason, boiIsrefund, boiRefundremark);
			
			String oId[] = ids.split(",");
			for (String id : oId) {
				this.orderLogDao.saveOrderLog(ConstantLocation.LOG_BATCH_QU_X, id, lolUtype, lolUser, 
						lolAddress, new Date(), lolOremark);
			}
		}
	}
	
	/**
	 * 批量删除订单及其相关信息
	 * @author liuzhi
	 * @param ids
	 * @param lolUtype
	 * @param lolUser
	 * @param lolAddress
	 */
	public void delOrdersByIds(String ids, String lolUtype, String lolUser, String lolAddress){
		if(StringUtil.isNotNullOrEmpty(ids)){
			this.orderDao.delOrdersByIds(ids);
			
			this.sysLogDao.saveSystemLogs(lolUser, lolAddress, ConstantLocation.LOG_BATCH_DELETE, 
					"delOrdersByIds", ConstantLocation.LOG_BATCH_DELETE, 
					"BaseOrderInfo,BaseOrderGoods,LogOrderLog");
		}
	}
	
	/**
	 * 根据区域编号或父级区域编号获得区域列表
	 * @author liuzhi
	 * @param bdrParent
	 * @return
	 */
	public String getRegionInfoList(String bdrId, String bdrParent){
		return this.orderDao.getRegionInfoList(bdrId, bdrParent);
	}
	
	/**
	 * 更新收货人信息
	 * @author liuhzi
	 * @param order
	 * @return
	 */
	public int updateConsigneeInfo(BaseOrderInfo order, String lolUtype, String lolUser, 
			String lolAddress){
		int count = 0;
		
		if(StringUtil.isNotNullOrEmpty(order) && StringUtil.isNotNullOrEmpty(order.getBoiId())){
			count = this.orderDao.updateConsigneeInfo(order);
			
			this.orderLogDao.saveOrderLog(ConstantLocation.LOG_ORDER_EDIT, order.getBoiId(), lolUtype,  
					lolUser, lolAddress, new Date(), ConstantLocation.LOG_EDIT_CONSIGNEE);
		}
		
		return count;
	}
	
	public String findOrderList(int pageIndex, int pageSize,
			String boiConsignee, String shopname, String boiMember,
			String boiMobile, String boiOrder, String boiOtimes,
			String boiOtimee,String boiShip) {
		return orderDao.findOrderList(pageIndex, pageSize, boiConsignee,
				shopname, boiMember, boiMobile, boiOrder, boiOtimes, boiOtimee,boiShip);
	}

	public String findOrderProductList(int pageIndex, int pageSize,
			String orderid) {
		return orderDao.findOrderProductList(pageIndex, pageSize, orderid);
	}

	public void updateOrderState(String boiId, String currentState,
			String optionState,String optionChange,String ip,String user) {
		orderDao.updateOrderState(boiId, currentState, optionChange);
	}

	public List findExportOrder(String boiConsignee, String shopname,
			String boiMember, String boiMobile, String boiOrder,
			String boiOtimes, String boiOtimee,String boiShip) {
		return orderDao.findExportOrder(boiConsignee, shopname, boiMember,
				boiMobile, boiOrder, boiOtimes, boiOtimee,boiShip);
	}

	public List queryHqlIn(List<String> list) {
		String hql="from BaseOrderInfo where boiId IN (:alist)";
		return orderDao.getOrderList(hql, list);
	}

	public String getGoodsCombobox(String value) {
		return orderDao.getGoodsCombobox(value);
	}

	public String showGoodsInfoById(String bgiId) {
		return orderDao.showGoodsInfoById(bgiId);
	}

	public String showGoodsAttrById(String bgiId) {
		return orderDao.showGoodsAttrById(bgiId);
	}

	public void saveOrderGoods(String jsonStr, String boiGpay, String boiId, String lolUtype,
			String lolUser, String lolAddress) {
		int count = orderDao.saveOrderGoods(jsonStr,boiGpay,boiId);
		
		if (count > 0) {
			this.orderLogDao.saveOrderLog(ConstantLocation.LOG_ORDER_EDIT, boiId, lolUtype,  
					lolUser, lolAddress, new Date(), ConstantLocation.LOG_EDIT_GOODS);
		}
	}

}
